# import sympy as sp


# x,y = sp.symbols('x y')
# f = x ** 2 + y ** 2 - 2 * x * y
# x_old = -10
# y_old = -1
# f_old = f.subs({x:x_old,y:y_old})
# while(True):
#     delta1 = 0.001
#     delta = -0.001
#     x_new = x_old + delta1
# #3.根据第二步算出新的y值
#     y_new = y_old + delta
#     f_new = x_new ** 2 + y_new ** 2 -2 * x_new * y_new
# #4.根据2的x_new算出新的y_new
# #5.比较y_new和y_old

#     if(f_new < f_old):
#         y_old = y_new
#         x_old = x_new
#         f_old = f_new
#     else:
#         print(x_old,y_old,f_old)
#         break











# 什么叫梯度下降
# 如果要求一个函数的极小值，只需将自变量不停的减导数，一定能来到极小值点
# 如何用梯度下降求解 y = x ** 2 的极小值 
import numpy as np
import sympy as sp
import random as r

x = r.randint(-100,1000)        #从该范围生成随机数(起始点)
learn_rate = 0.1                #学习率

epochs = 10000                  #设置迭代次数
for i in range(epochs):
    x = x - 2 * x * learn_rate  #梯度下降
    y = x ** 2
    if y == 0:
        # prinxt(x)
        # print(y)
        break
    # print(y)

# y = x ** 2
# print(y)